package com.aaa.web.service.impl.marketserviceimpl;

import com.aaa.entity.*;
import com.aaa.entity.repair.BackEntrepot;
import com.aaa.phone.systemconst.ISystemConst;
import com.aaa.util.EmpUtil;
import com.aaa.util.OrderNum;
import com.aaa.web.commons.EmpQuery;
import com.aaa.web.commons.Result;
import com.aaa.web.commons.WlCommonsCode;
import com.aaa.web.dao.imarketdao.IMarketManageDao;
import com.aaa.web.service.imarketservice.IMarketManageService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @ClassName MarketManageServiceImpl
 * @Description 销售管理
 * @Author caolinlin
 * @Date 2018/12/18 15:35
 **/
@Service
public class MarketManageServiceImpl implements IMarketManageService,ISystemConst {

    @Autowired
    private IMarketManageDao marketManageDao;


    /**
     * 获取全部订单数据
     * @param query
     * @param e
     * @return
     */
    @Override
    public Map getMarketManage(EmpQuery query, BackExpenseorder e) {

        //是否超管
        Employee emp = EmpUtil.getEmp();
        int admin = emp.getAdmin();

        //登陆的工作人员的学校id
        int schoolId = WlCommonsCode.isAdmin();

        //第几页
        int page = query.getPage();
        //数据条数
        int size = query.getLimit();
        PageHelper.startPage(page,size);
        Map m = new HashMap();
        //数据
        m.put("data",marketManageDao.getAllMarketManage(e,schoolId));
        m.put("isAdmin",admin);
        //数据总条数
        m.put("count",marketManageDao.getMarketManageCount(e,schoolId));
        //状态
        m.put("code",0);
        //返回的信息
        m.put("msg","");
        return m;
    }

    /**
     * 删除订单(订单添加错误,想取消订单)
     * 事务回滚    @Transactional
     * @param e
     * @return Result
     */
    @Override
    @Transactional
    public Result delMarketByOrderId(BackExpenseorder e) {
        //仅改变订单表中的状态
       /* marketManageDao.delMarketByOrderId(e);*/

        //登陆的工作人员的学校id
        int schoolId = WlCommonsCode.isAdmin();

        //获取当前时间
        Date date = new Date();

        //根据订单号获得该订单内的所有数据
        List<BackExpenseorder> backExpenseorder = marketManageDao.getDetailMarketByOrderId(e);
        for (int i=0;i<backExpenseorder.size();i++){
            Map<String,Object> map= (Map<String, Object>) backExpenseorder.get(i);
            int goodsid= (int) map.get("goodsid");
            int number=(int) map.get("number");

            //将库存中创建订单时减去的商品数量再加回去
            int k=marketManageDao.delMarketByOrderIdOnEntrepot(goodsid,number,schoolId);

        }
        //删除订单表中的数据
        int m=marketManageDao.delMarketByOrderIdOnOrder(e);
        //修改订单记录表中该订单号的数据的isdelete状态为已删除
        int j=marketManageDao.delMarketByOrderIdOnRecord(e);
        Result r=new Result();
        if(m > ISystemConst.ZERO){
           r.setMsg("删除成功");
        }else {
            r.setMsg("删除失败");
        }
        return r;

    }

    /**
     * 获取该订单的详细信息
     * @param e
     * @return
     */
    @Override
    public Map getDetailMarketManage(BackExpenseorder e) {
        Map m = new HashMap();
        //数据
        m.put("data",marketManageDao.getDetailMarketManage(e));
        //数据总条数
        m.put("count",ISystemConst.ZERO);
        //状态
        m.put("code",ISystemConst.ZERO);
        //返回的信息
        m.put("msg","");
        return m;

    }

    /**
     * 获取消费类型列表
     * @return
     */
    @Override
    public List<AppExpensetype> getExpenseTypeList() {
        return marketManageDao.getExpenseTypeList();
    }

    /**
     * 获取商品类型列表
     * @return
     */
    @Override
    public List<BackGoodstype> getGoodStypeAddList() {
        return marketManageDao.getGoodStypeAddList();
    }

    /**
     * 生成新订单号
     * orderNum工具类生成订单号
     * @return
     */
    @Override
    public Result getNewOrderNumber() {
        String newOrderNumber= OrderNum.createOrderNumber();
        Result result = new Result();
        result.setMsg(newOrderNumber);
        return result;
    }

    /**
     * 批量添加订单
     * @param mapList
     * @return
     */
    @Override
    public Result addMarketManage( List<List<Map<String,Object>>> mapList) {
        //订单号
        String expenseOrder = (String)mapList.get(ZERO).get(ZERO).get("expenseorder");
        //电话号码
        String telephone = (String)mapList.get(ONE).get(ZERO).get("telephone");
        //表格信息
        List<Map<String,Object>> backEntrepot = mapList.get(TWO);
        List<Map<String,Object>> arrgoodsNumber = mapList.get(THREE);
        List<Map<String,Object>> arrcost = mapList.get(FOUR);

        //获取当前时间
        Date now = new Date();

        /**
         * 计算总价
         */
        double countCost=ZERO;
        for (int i = 0; i < arrcost.size(); i++) {
            countCost+=Double.parseDouble((String) arrcost.get(i).get("cost"));
        }

        Employee emp = EmpUtil.getEmp();
        //登陆的工作人员的id(由登录人员的id得到学校id,由学校获取该学校所有工作人员的id)
        int employeeid=emp.getEmployeeid();

        //登陆的工作人员的学校id
        int schoolId = WlCommonsCode.isAdmin();

        /**
         * 添加到订单表中的数据集合
         */
        List<BackEntrepot> backEntrepots = new ArrayList<>();
        for (int i = 0; i < backEntrepot.size(); i++) {
            BackEntrepot bn = new BackEntrepot();

            //学校id
            bn.setSchoolId((int)backEntrepot.get(i).get("school_id"));
            //商品名
            bn.setGoodsname((String)backEntrepot.get(i).get("goodsname"));
            //商品类型
            bn.setGoodstypeId((int)backEntrepot.get(i).get("goodstype_id"));
            //商品id
            bn.setId((int)backEntrepot.get(i).get("id"));
            //商品库存
            bn.setGoodsquantity((int)backEntrepot.get(i).get("goodsquantity"));
            //订单号
            bn.setExpenseorder(expenseOrder);
            //电话号
            bn.setTelephone(telephone);
            //每件商品的购买数量
            /*Object goodsNumber = arrgoodsNumber.get(i).get("goodsNumber");*/
            bn.setGoodsNumber(Integer.parseInt((String) arrgoodsNumber.get(i).get("goodsNumber")));

            //一种商品的总价(单价*数量)
            bn.setCost(Double.parseDouble((String)arrcost.get(i).get("cost")));
            //总花费
            bn.setCountCost(countCost);
            //登录人id
            bn.setEmployeeid(employeeid);

            bn.setCreatetime(now);
            bn.setUpdatetime(now);

            backEntrepots.add(bn);

            //修改库存数量
            int u=marketManageDao.updateMarketManageByOrder(bn);
        }

        //添加到订单表
        int ret=marketManageDao.addMarketManage(backEntrepots);
        //添加到订单记录表
        int a=marketManageDao.addMarketManageRecorder(backEntrepots);

        Result r=new Result();
        if(ret>ZERO){
            //成功
            r.setCode(SUCCESS_CODE);
        }else {
            //失败
            r.setCode(DEFEAT_CODE);
        }
        return r;

    }

    /**
     * 根据 商品种类id 在 仓库表 中查询出 本地的 对应 商品种类 的商品
     * @param goodstype_id
     * @return
     */
    @Override
    public List<BackEntrepot> selGoodsListById(int goodstype_id) {
        //登陆的工作人员的学校id
        int schoolId = WlCommonsCode.isAdmin();
        return marketManageDao.selGoodsListById(goodstype_id,schoolId);
    }

    /**
     * 获取车辆列表
     * @return
     */
    @Override
    public List<BackEntrepot> getCarsList() {
        //登陆的工作人员的学校id
        int schoolId = WlCommonsCode.isAdmin();
        return marketManageDao.getCarsList(schoolId);
    }

    /**
     * 获取仓库商品信息
     * @param goodstypeId
     * @return
     */
    @Override
    public Map getEntrepotGoods(int goodstypeId) {
        //登陆的工作人员的学校id
        int schoolId = WlCommonsCode.isAdmin();
        Map m = new HashMap();
        //数据
        m.put("data",marketManageDao.getEntrepotGoods(schoolId,goodstypeId));
        //数据总条数
        m.put("count",ZERO);
        //状态
        m.put("code",ZERO);
        //返回的信息
        m.put("msg","");
        return m;
    }


}
